/**
 * 
 */
package com.scpii.universal.hibernate.dao.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.BindContentAttributeDto;
import com.scpii.universal.hibernate.dao.BindContentAttributeDao;
import com.scpii.universal.hibernate.entity.BindContentAttributeTab;


/**
 * 
 * universalWeb
 * @author zhangqiang
 * 2012-7-10下午05:21:35
 */
@Repository
public class BindContentAttributeDaoImpl extends BaseDaoImpl<BindContentAttributeTab, Integer> implements BindContentAttributeDao{

	@SuppressWarnings("unchecked")
	@Override
	public List<BindContentAttributeDto> getBindContAttrsByViewContId(
			int viewContId) {
		StringBuffer hql = new StringBuffer();
		hql.append("select new com.scpii.universal.dto.BindContentAttributeDto(");
		hql.append("b.intBindContAttributeId,b.intAttriId,b.intOrder)");
		hql.append(" from BindContentAttributeTab b where b.intDeleteSign=?");
		hql.append(" and b.intViewContId=?");
		return getHibernateTemplate().getSessionFactory().getCurrentSession()
				.createQuery(hql.toString()).setParameter(0,
						GenericConstants.DELETE_SIGN_EXIST).setParameter(1,
						viewContId).list();
	}

	@Override
	public void deleteByAttrId(int attrId) {
		String hql = "update BindContentAttributeTab b set"
				+ " b.intDeleteSign=?,b.datUpdateDatetime=? where b.intAttriId=?";
		getHibernateTemplate()
				.getSessionFactory()
				.getCurrentSession()
				.createQuery(hql)
				.setParameter(0, GenericConstants.DELETE_SIGN_DELETED)
				.setParameter(1,
						new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date())).setParameter(2, attrId)
				.executeUpdate();
	}

	@Override
	public void delete(Integer id) {
		String hql = "update BindContentAttributeTab b set"
				+ " b.intDeleteSign=?,b.datUpdateDatetime=? where b.intViewContId=?";
		getHibernateTemplate()
				.getSessionFactory()
				.getCurrentSession()
				.createQuery(hql)
				.setParameter(0, GenericConstants.DELETE_SIGN_DELETED)
				.setParameter(
						1,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
								.format(new Date())).setParameter(2, id)
				.executeUpdate();
	}

	
}
